package utils

import (
	"errors"
	"os"
	"path"
	"strings"
	"time"

	"github.com/google/uuid"
)

func GenPathBaseRTime(prefix, prjName, prjUser string) (string, error) {
	raw_ti := getTS()
	ret_path := path.Join(prefix, raw_ti+"-"+prjName+"-"+prjUser)
	if _, err := os.Stat(ret_path); err == nil {
		return "", errors.New("duplicate project path")
	}
	if err := os.Mkdir(ret_path, os.ModeDir); err != nil {
		return "", err
	}
	return ret_path, nil
}

func GenUUID() string {
	return uuid.New().String()
}

func GenLogPath(base string) (string, error) {
	log_path := path.Join(base, "Log")
	if _, err := os.Stat(log_path); err != nil {
		if err = os.Mkdir(log_path, os.ModeDir); err != nil {
			return "", err
		}
	}
	log_file := path.Join(log_path, getTS()+".log")
	if _, err := os.Stat(log_file); err != nil {
		if _, err = os.Create(log_file); err != nil {
			return "", err
		}
	}
	return log_file, nil
}

func getTS() string {
	raw_ti := strings.Split(time.Now().String(), ".")[0]
	raw_ti = strings.Replace(raw_ti, " ", "-", -1)
	return strings.Replace(raw_ti, ":", "-", -1)
}
